


playerDataPoints={}
playerDataClicks={}

function reset(plr)
    playerDataClicks[getElementData(plr,"player:data").uid] = 0
end

addEvent("cmta_zarabianie:get_info", true)
addEventHandler("cmta_zarabianie:get_info", resourceRoot, function()

    
    local data = getElementData(client,"player:data")

        local result = exports["cmta-db"]:dbGet("SELECT * FROM cmta_users WHERE id=?", data.uid)
        
        if not playerDataPoints[result[1]["id"]] and result[1]["hidden_points"] > 0 then
            playerDataPoints[result[1]["id"]] = result[1]["hidden_points"]
        end
        
        if not playerDataClicks[result[1]["id"]] and result[1]["dailyclicks"] > 0 then
        playerDataClicks[result[1]["id"]] = result[1]["dailyclicks"]
        end

        triggerClientEvent(client,'cmta_zarabianie:info',resourceRoot, (playerDataPoints[data.uid] or 0), (playerDataClicks[data.uid] or 0))
end)


addEvent("cmta:zarabianie:ulepsz", true)
addEventHandler("cmta:zarabianie:ulepsz", resourceRoot, function(typ,price)
    if getElementData(client,"player:data").points < price then return end
    if typ == "1" then
        local data = getElementData(client,"player:data")
        data.gainscore = data.gainscore+100
        setElementData(client,"player:data",data)
        exports["cmta-db"]:dbSet("update cmta_users set gainscore=gainscore+? where id=?",100,getElementData(client,"player:data").uid)
    elseif typ == "2" then
        local data = getElementData(client,"player:data")
        data.timemultiplier = data.timemultiplier-20
        setElementData(client,"player:data",data)
        exports["cmta-db"]:dbSet("update cmta_users set timemultiplier=timemultiplier-? where id=?",20,getElementData(client,"player:data").uid)
    end
        local data = getElementData(client,"player:data")
        data.points = data.points-tonumber(price)
        setElementData(client,"player:data",data)
        exports['cmta-notyfikacje']:sendNotification(client,"#ccc000Sukces!\n#ffffffPomyślnie zakupiono ulepszenie.","success",3000)
        triggerClientEvent(client,'cmta:zarabianie:refresh',resourceRoot)
    end)


addEvent("cmta_zarabianie:kick", true)
addEventHandler("cmta_zarabianie:kick", resourceRoot, function()
    kickPlayer(client,"[ANTYCHEAT] Zostałeś wyrzucony z serwera za spam klikaniem.")
end)

addEvent("cmta_zarabianie:punkty", true)
addEventHandler("cmta_zarabianie:punkty", resourceRoot, function(amount)
    local data = getElementData(client,"player:data")
    playerDataPoints[data.uid] = (playerDataPoints[data.uid] or 0)+(amount or (getElementData(client,"player:data").gainscore/1000))
    playerDataClicks[data.uid] = (playerDataClicks[data.uid] or 0)+1
    triggerClientEvent(client,'cmta_zarabianie:info',resourceRoot, playerDataPoints[data.uid], playerDataClicks[data.uid])
end)

addEvent("cmta_zarabianie:odbierzpunkty", true)
addEventHandler("cmta_zarabianie:odbierzpunkty", resourceRoot, function()
    local data = getElementData(client,"player:data")
    if playerDataPoints[data.uid] and tonumber(playerDataPoints[data.uid]) > 0 then
        data.points = data.points+tonumber(playerDataPoints[data.uid])
        setElementData(client,"player:data",data)
        exports['cmta-notyfikacje']:sendNotification(client,"#ccc000Sukces!\n#ffffffPomyślnie odebrałeś "..(playerDataPoints[data.uid]).." pkt.","success",3000)
        playerDataPoints[data.uid] = 0
        exports["cmta-db"]:dbSet("update cmta_users set hidden_points=0 where id=?",getElementData(client,"player:data").uid)
        triggerClientEvent(client,'cmta_zarabianie:info',resourceRoot, (playerDataPoints[data.uid] or 0), (playerDataClicks[data.uid] or 0))

    else
        exports['cmta-notyfikacje']:sendNotification(client,"#FF0000Wykryto nieprawidłowość!\n#ffffffNie masz punktów do odebrania.","error",4000)
    end
end)
--[[
addEvent("cmta_zarabianie:get_info", true)
addEventHandler("cmta_zarabianie:get_info", resourceRoot, function()
    local data = getElementData(client,"player:data")
    triggerClientEvent(client,'cmta_zarabianie:info',resourceRoot, (playerDataPoints[data.uid] or 0), (playerDataClicks[data.uid] or 0))
end)]]

function stopScript( res )
    if res == getThisResource() then
		for i, v in pairs(playerDataPoints) do
           exports["cmta-db"]:dbSet("update cmta_users set hidden_points=? where id=?",v,i)
		end
		for i, v in pairs(playerDataClicks) do
            exports["cmta-db"]:dbSet("update cmta_users set dailyclicks=? where id=?",v,i)
		end
	end
end 
addEventHandler ( "onResourceStop", resourceRoot, stopScript )